package com.qiantu.phone.ui.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.qiantu.api.entity.FloorBean;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppAdapter;
import com.qiantu.phone.ui.activity.RoomManagementActivity;

/**
 *    author : Android 轮子哥
 *    github : https://github.com/getActivity/AndroidProject
 *    time   : 2019/09/22
 *    desc   : 房间标签数据列表
 */
public final class RoomFloorAdapter extends AppAdapter<FloorBean> {

    public RoomFloorAdapter(Context context) {
        super(context);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder();
    }

    private final class ViewHolder extends AppAdapter<?>.ViewHolder {

        private final TextView tvFloorName;
        private final View floorLayout;
        private final View btnMore;
        private final RecyclerView roomRecyclerView;
        private final View btnAddRoom;

        private ViewHolder() {
            super(R.layout.room_floor_item);
            floorLayout = findViewById(R.id.floor_layout);
            tvFloorName = findViewById(R.id.tv_floor_name);
            btnMore = findViewById(R.id.btn_more);
            roomRecyclerView = findViewById(R.id.room_recycler_view);
            btnAddRoom = findViewById(R.id.btn_add_room);
        }

        @Override
        public void onBindView(int position) {
            FloorBean floor = getItem(position);
            //默认楼层不显示
            if(floor.getType() == 1 || TextUtils.isEmpty(floor.getFloorName())){
                floorLayout.setVisibility(View.GONE);
            }else{
                floorLayout.setVisibility(View.VISIBLE);
                tvFloorName.setText(floor.getFloorName());
            }
            btnMore.setTag(floor);
            if(floor.getRooms() == null || floor.getRooms().size() == 0){
                btnAddRoom.setVisibility(View.VISIBLE);
                MineRoomAdapter roomAdapter = (MineRoomAdapter) roomRecyclerView.getAdapter();
                if(roomAdapter != null){
                    roomAdapter.setData(null);
                }
                roomRecyclerView.setVisibility(View.GONE);
            }else{
                MineRoomAdapter roomAdapter = (MineRoomAdapter) roomRecyclerView.getAdapter();
                if(roomAdapter == null){
                    roomAdapter = new MineRoomAdapter(getContext());
                    roomRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
                }
                roomAdapter.setData(floor.getRooms());
                roomAdapter.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(RecyclerView recyclerView, View itemView, int position) {
                        ((RoomManagementActivity)getContext()).gotoEditRoomActivity(floor, floor.getRooms().get(position));
                    }
                });
                roomRecyclerView.setAdapter(roomAdapter);
                roomRecyclerView.setVisibility(View.VISIBLE);
                btnAddRoom.setVisibility(View.GONE);
            }
        }
    }
}